local gets = {}
local sets = {}
Sprite = {
    name = "sprite",
    props = {
        _drawType = SpriteDrawType.Simple
    },
    dependencies = {
        Renderer
    },
    gets = gets,
    sets = sets
}
function Sprite:ctor(url)
    self.drawType = SpriteDrawType.Simple
    if url then
        self:setUrl(url)
    end
end
local assemblers = {
    [SpriteDrawType.Simple] = AssemblerSpriteSimple,
    [SpriteDrawType.Scale9] = AssemblerSpriteScale9
}

function gets:drawType()
    return self._drawType
end

function sets:drawType(key, val)
    self._drawType = val
    self._assembler = assemblers[val].new(self)
end

function gets:spriteFrame()
    return self._spriteFrame or SpriteFrame.default
end

function sets:spriteFrame(key, val)
    self._spriteFrame = val
    self.material:setTexture(val.image)
end

local imageMaterials = {}
function Sprite:createDefaultMaterial()
    if not imageMaterials[self.spriteFrame.image] then
        local mat = Material.new("default")
        mat:setTexture(self.spriteFrame.image)
        imageMaterials[self.spriteFrame.image] = mat
    end
    return imageMaterials[self.spriteFrame.image]
end

function Sprite:setNativeSize()
    self:wh(self.spriteFrame:getNativeSize())
end

function Sprite:setUrl(url)
    if type(url) == "string" then
        self.spriteFrame = ResourceSystem:loadSpriteFrame(url)
    end
end

function Node.Sprite(url)
    local sp = Node.new():addComponent(Sprite)
    sp.name = "sprite"
    sp:setUrl(url)
    -- 按直觉逻辑，一个能看得见的对象本身应该是会阻挡点击的，所以默认会将sprite的交互打开
    sp.interactable = true
    return sp
end

function Node.Scale9(url)
    local node = Node.Sprite(url)
    node.drawType = SpriteDrawType.Scale9
    return node
end